<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>封装</title>
</head>
<body>
<script>
    //字面量方式
    var obj1 = {
        name: "财哥",
        sex: "男",
        run: function() {
            console.log("跑");
        }
    }
    console.log(obj1.name);
    obj1.run();
    //使用Object来创建
    var obj2 = new Object();
    obj2.name = "财哥";
    obj2.sex = "男";
    obj2.run = function(){
        console.log("跑");
    }
    console.log(obj2.name);
    obj2.run();
    //使用工厂模式创建
    function createPerson(p_name, p_sex, p_run) {
        var temPerson = new Object();
        temPerson.name = p_name;
        temPerson.sex = p_sex;
        temPerson.run = p_run;
        return temPerson;
    }
    var obj3 = createPerson("财哥", "男", new Function("console.log('跑')"));
    var obj4 = createPerson("发财哥", "Boy", new Function("console.log('飞跑')"));
    console.log(obj4.name);
    console.log(obj4.sex);
    obj4.run();
    //使用构造函数创建
    function Student(name, sex){
        this.name = name;
        this.sex = sex;
        this.run = function(){
            console.log(this.name + "跑");
        }
    }
    var obj5 = new Student("财哥", "男");//没有类的概念
    console.log(obj5.name);
    obj5.run();
    //使用原型创建
    function Person() {
    }
    Person.prototype.name = "发财哥";
    Person.prototype.sex = "boy";
    Person.prototype.run = function() {
        console.log(this.name + "跑");
    };
    var obj6 = new Person();
    console.log(obj6);
    obj6.run();

    //经典组合：构造函数+原型模式 prototype:原型
    function Person(name, sex){
        this.name = "财哥";
        this.sex = "男";
    }
    Person.prototype.run = function() {
        console.log(this.name + "跑");
    };
    var obj7 = new Person("财哥", "男");
    console.log(obj7);
    obj7.run();


    
    // 属性：数据属性，访问器属性

    //this this就是获得运行当前函数所在的运行环境 this不是变量，所以不能更改它的值。call() apply() bind()
</script>    
</body>
</html>